package com.ogms.modelservice;

import java.io.IOException;
import java.util.List;

public class Test {

	public static void main(String[] args) throws IOException {
		Server server = ServerFactory.createServer("172.21.212.119", 8060);
		if(server.connect() == 1){
			System.out.println("Hello my sdk!");
			ServiceAccess pServiceAccess = server.getServiceAccess();
			List<ModelService> list_ms = pServiceAccess.getModelServicesList();

			for(int i = 0; i < list_ms.size(); i++){
				ModelService ms = list_ms.get(i);
				if(ms == null) continue;
				System.out.println(ms.getServiceOID() + " - " + ms.getServiceName() + " - " + ms.getServiceType());
			}
			System.out.println("test success!");

			//上传数据
			Data pData = pServiceAccess.uploadDataByFile("F:\\Test\\Input.inp", "Input.inp");
			if(pData != null){
				System.out.println(pData.getID() + " - " + pData.getSize() + " - " + pData.getGenerationDateTime() + " - " + pData.getTag());
				DataConfiguration pDataconfig = pServiceAccess.createDataConfig();
				pDataconfig.insertData("9949226b-958f-453f-8bc4-a8a03246d412", "inpData", pData.getID(), false);
				ModelService pMservice = pServiceAccess.getModelServiceByOID("5b9a7169623cbf0a544a7653");
				String recordid = pMservice.invoke(pDataconfig);
				System.out.println(recordid);
				ModelServiceRecord pRecord = pServiceAccess.getModelServiceRecordByID(recordid);


				//System.out.println("fuck no bug!!!");
				int msrstatus = pRecord.getStatus().getCode();
				System.out.println("model status is " + msrstatus);
				while(msrstatus == 0){
					try{
						Thread.sleep(2000);
					}catch (InterruptedException e){
						e.printStackTrace();
					}
					pRecord.refresh();
					//打印log信息
					List<RunningLog> list_logs = pRecord.getLogs();
					for(int j = 0; j < list_logs.size(); j++){
						RunningLog log = list_logs.get(j);
						System.out.println(log.getType() + " - " + log.getState() + " - " + log.getEvent() + " - " + log.getMessage());
					}
					msrstatus = pRecord.getStatus().getCode();
					System.out.println("Model Run Status is " + msrstatus);

				}
				System.out.println("TouchAir model has been finished!");

				//测试保存文件
				for(int t = 0; t < pRecord.getOutputData().getCount();t++){
					DataConfigItem item = pRecord.getOutputData().getItem().get(t);
					String dataid = item.data;
					Data outputdata = pServiceAccess.getDataServiceByID(dataid);
					String datavalue = outputdata.getValue();
					//根据value值获取文件后缀
					String ext = datavalue.substring(datavalue.lastIndexOf(".") + 1);
					System.out.println(ext);
					outputdata.saveAs("E:\\NativeTest\\TestData\\TouchAir\\" + item.event + "." + ext);
				}
			}
		}
	}

}
